# -*- encoding:utf-8 -*-
# @time: 2023/4/14 21:03
# @author: Maxs_hu
from django.db.models import Q
from django.shortcuts import render

from utils.group import Option, NbSearchGroup
from utils.pager import pagination
from web import models


def transaction_list(request):
    """ 我的交易记录 """

    # 第一步：配置和传参
    search_group = NbSearchGroup(
        request,
        models.TransactionRecord,
        Option('charge_type'),  # choice字段名
    )

    # 第二步：获取条件 .filter(**search_group.get_condition)
    keyword = request.GET.get('keyword', "")

    q = Q()
    if keyword:
        keyword = keyword.strip()
        q.connector = 'OR'
        q.children.append(('order_oid__contains', keyword))

    queryset = models.TransactionRecord.objects.filter(q).filter(**search_group.get_condition).filter(
        customer_id=request.user_obj.id, active=1).order_by(
        '-id')
    # 封装的分页对象
    pager = pagination(request, queryset)
    context = {
        'show_data': pager.show_data,
        'pager_string': pager.html(),
        'keyword': keyword,
        "search_group": search_group
    }
    return render(request, 'transaction_list.html', context)
